from enum import Enum

from db import init_db,get_db,close_db
from sqlmodel import Session,select
from rbac_models import Role,Permission,User

class RoleEnum(str,Enum):
    DRIVER = "driver"
    CAPTAION = "captain"
    SCHEDULER = "scheduler"
    ADMIN = "admin"
def create_roles(session:Session):
    role1 = Role(role_name="driver")
    role2 = Role(role_name="captain")
    role3 = Role(role_name="scheduler")
    role4 = Role(role_name="admin")
    session.add_all([role1,role2,role3,role4])
    session.commit()
    print(role1)
    print(role2)

def create_permissions(session:Session):
    user_create = Permission(resource="user",action="create")
    user_read = Permission(resource="user",action="read")
    user_update = Permission(resource="user",action="update")
    user_delete = Permission(resource="user",action="delete")
    session.add_all([user_create,user_read,user_update,user_delete])
    session.commit()

def bind_role_permissions(session:Session):
    roles = [1,2,3,4]
    perms = session.exec(
        select(Permission)
    ).all()
    user_result = session.exec(
        select(Role).where(Role.id.in_(roles)) #type:ignore
    )
    for role in user_result.all():
        role.permissions = list(perms)
    session.add_all(user_result)
    session.commit()

def bind_user_roles(session:Session):
    role_result = session.exec(
        select(Role)
    ).all()
    user_result = session.exec(
        select(User)
    ).all()
    for index,user in enumerate(user_result):
        user.roles = [role_result[index]]
    session.add_all(user_result)
    session.commit()
    print(user_result[0].roles)

def create_users(session:Session):
    user1 = User(username="admin",hashed_password="123456")
    user2 = User(username="user1",hashed_password="123456")
    user3 = User(username="user2",hashed_password="123456")
    user4 = User(username="user3",hashed_password="123456")
    session.add_all([user1,user2,user3,user4])
    session.commit()
def get_user_permissions(session:Session,user_id:int):
    user_result = session.exec(
        select(User).where(User.id==user_id) #type:ignore
    ).one_or_none()
    if not user_result:
        return []
    print(f"获取用户:{user_result.roles}")
    for role in user_result.roles:
        print(role.permissions)

def get_usres_by_role(session:Session,role_id:int):
    role_result = session.exec(
        select(Role).where(Role.id==role_id) #type:ignore
    ).one_or_none()
    print(role_result.users)

def add_role_for_user(session:Session,user_id:int,role_id:int):
    """ 为用户添加角色"""
    user_result = session.exec(
        select(User).where(User.id==user_id) #type:ignore
    ).one_or_none()
    print(f"添加用户角色前：{len(user_result.roles)}")
    role_result = session.exec(
        select(Role).where(Role.id==role_id) #type:ignore
    ).one_or_none()
    user_result.roles.append(role_result)
    session.add(user_result)
    session.commit()
    print(f"添加用户角色后：{len(user_result.roles)}")
    # print(user_result.roles)

def add_permission_for_role(session:Session,role_id:int,permission_id:int):
    role_result = session.exec(
        select(Role).where(Role.id==role_id) #type:ignore
    ).one_or_none()
    permission_result = session.exec(
        select(Permission).where(Permission.id==permission_id) #type:ignore
    ).one_or_none()
    role_result.permissions.append(permission_result)
    session.add(role_result)
    session.commit()
    print(role_result.permissions)

def remove_permission_from_role(session:Session,role_id:int,permission_id:int):
    """ 删除角色权限"""
    permission_result = session.exec(
        select(Permission).where(Permission.id==permission_id) #type:ignore
    ).one_or_none()
    role_result = session.exec(
        select(Role).where(Role.id==role_id) #type:ignore
    ).one_or_none()
    if not permission_result or not role_result:
        return
    role_result.permissions.remove(permission_result)
    session.add(role_result)
    session.commit()


def main():
    init_db()
    with get_db() as session:
        # create_roles(session)
        # create_permissions(session)
        # create_users(session)
        # bind_role_permissions(session)
        # bind_user_roles(session)
        # get_user_permissions(session,3)
        # get_usres_by_role(session,2)
        # add_role_for_user(session,1,4)
        # add_permission_for_role(session,1,4)
        remove_permission_from_role(session,1,4)
    close_db()

if __name__ == "__main__":
    main()